package com.wwwc.index.web.servlet;

import java.io.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;
import java.text.DecimalFormat;

import javax.servlet.ServletRequestWrapper;
import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;

import com.wwwc.util.web.*;
import com.wwwc.index.web.ejb.database.*;

public class ScheduleDAdmin extends ScheduleD {
    public StringBuffer main(HttpServletRequest request, String call_path, String mid) {
	String aid = adminCallBack;
	StringBuffer sbf = new StringBuffer();
	try {

	    String who = request.getParameter("who");
	    if(who == null || who.length()<4) {
		return getEditUserName(call_path, mid, aid, "who");
	    }
	    else {
		sbf.append(getEditUserRecordInterface(call_path, mid, aid, who));
		String what = request.getParameter("what");
	      
		if(what != null) {
		    String which_year = request.getParameter("which_year");  
		    String which_part = request.getParameter("which_part");
		    String which_bid  = request.getParameter("which_bid");
		    String which_fid  = request.getParameter("which_fid");
		    who               = request.getParameter("who");

		    if(what.equals("reset")) {
			String status = request.getParameter("status");
			if(status == null) {
			    sbf.append(MyHtml.error("Status not set"));
			}
			else {
			    if(!update1099Status(who, which_year, which_bid, Integer.parseInt(status))) {
				sbf.append("Reset status error. Please try again.");
			    }
			    else {
				sbf = null;
				sbf = getEditUserRecordInterface(call_path, mid, aid, who);
			    }
			}
		    }

		    if(what.equals("view")) {
			sbf.append(viewHolding(request, what, who, which_year, which_part, which_bid, which_fid));
		    }

		    if(what != null && what.equals("edit")) {
			String submit = request.getParameter("submit");
			if(submit != null && submit.equals("Try")) {
			    sbf.append(updateHolding(request));
			}
			if(submit != null && submit.equals("Add")) {
			    sbf.append(addHoldingToDetail(request));
			}
			sbf.append(editHolding(request, call_path, mid, aid, what, who, which_year, which_part, which_bid, which_fid));
		    }
		}
	    }
	}
	catch (Exception e) {
	    System.out.println("ScheduleDAdmin:main:Error:"+e);
	}
	return sbf;
    }

    private StringBuffer  getEditUserRecordInterface(String call_path, String mid, String aid, String who) {
	StringBuffer sbf = new StringBuffer();
	try {
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }

	    ArrayList a = null;
	    ArrayList alist = ejbdb.selectUserScheduleDInfo(who);

	    sbf.append("<BR><BR>");
	    sbf.append("<TABLE border=1  cellSpacing=0 cellPadding=2 width=90%>");
	    sbf.append("<TR><TD align=center>STATUS</TD>");
	    sbf.append("<TD align=center>USER</TD>");
	    sbf.append("<TD align=center>YEAR</TD>");
	    sbf.append("<TD align=center colspan=2>PRE-HOLDING</TD>");
	    sbf.append("<TD align=center colspan=2>THIS-HOLDING</TD>");
	    sbf.append("<TD align=center>BROKER</TD>");
	    sbf.append("<TD align=center>TOTAL SOLD</TD>");
	    sbf.append("<TD align=center>STATUS</TD>");
	    sbf.append("</TR>");

	    String parent_link = call_path+"?mid="+mid+"&aid="+aid+"&who="+who;

	    if(alist != null && !alist.isEmpty()) {
		Iterator it1 = alist.iterator();
		Iterator it2 = null;
		String fid    = null;
		String bid    = null;
		String yid   = null;
		String t_sold = null;
		String status = null;
		String url = null;
		String cyear = null;
		double y_total = 0.0;
		boolean down = true;

		while (it1.hasNext()) {
		    a = (ArrayList)it1.next();
		    it2    = a.iterator();
		    fid    = (String)a.get(0);
		    bid    = (String)a.get(1);
		    t_sold = (String)a.get(2);
		    yid    = (String)a.get(3);
		    status = (String)a.get(4);
		    
		    if(cyear != null && !cyear.equals(yid)) {
			sbf.append("<TR>");
			sbf.append("<TD align=center>&nbsp;</TD>");
			sbf.append("<TD align=center>&nbsp;</TD>");
			sbf.append("<TD align=center><B>"+cyear+"</B></TD>");
			
			url = parent_link+"&what=view&which_part=preh&which_year="+cyear+"&okid="+down;
			sbf.append("<TD align=center>"+MyHtml.makeLinkButton("View", url)+"</TD>");
			url = parent_link+"&what=edit&which_part=preh&which_year="+cyear+"&okid="+down;
			sbf.append("<TD align=center>"+MyHtml.makeLinkButton("Edit", url)+"</TD>");

			url = parent_link+"&what=view&which_part=thish&which_year="+cyear+"&okid="+down;
			sbf.append("<TD align=center>"+MyHtml.makeLinkButton("View", url)+"</TD>");
			url = parent_link+"&what=edit&which_part=thish&which_year="+cyear+"&okid="+down;
			sbf.append("<TD align=center>"+MyHtml.makeLinkButton("Edit", url)+"</TD>");

			sbf.append("<TD align=center>&nbsp;</TD>");
			sbf.append("<TD align=right>"+td.format(y_total)+"</TD>");
			if(down) {
			    sbf.append("<TD align=center><img src=/images/star_green.gif></TD>");
			}
			else {
			    sbf.append("<TD align=center><img src=/images/star.gif></TD>");
			}
			sbf.append("</TR>");
			
			y_total = 0.0;
		    }

		    if(Integer.parseInt(status) < 6) {
			down = false;
		    }

		    y_total += Double.parseDouble(t_sold);
		    
		    sbf.append("<TR>");
		    sbf.append("<FORM method=post action="+parent_link+">");
		    sbf.append("<input type=hidden name=what value=reset>");
		    sbf.append("<input type=hidden name=which_year value="+yid+">");
		    sbf.append("<input type=hidden name=which_fid value="+fid+">");
		    sbf.append("<input type=hidden name=which_bid value="+bid+">");
		    sbf.append("<TD align=center>");
		    sbf.append("<input type=text name=status value=0 size=1 style='font-size:8pt'>");
		    sbf.append("<input type=submit name=submit value=SET style='font-size:8pt'>");
		    sbf.append("</TD>");
		    sbf.append("</FORM>");
		    sbf.append("<TD align=center>"+who+"</TD>");
		    sbf.append("<TD align=center>"+yid+"</TD>");

		    url = parent_link+"&what=view&which_part=preh&which_year="+yid+"&which_fid="+fid+"&which_bid="+bid;
		    sbf.append(MyHtml.makeLinkCell("View", url));
		    url = parent_link+"&what=edit&which_part=preh&which_year="+yid+"&which_fid="+fid+"&which_bid="+bid;
		    sbf.append(MyHtml.makeLinkCell("Edit", url));

		    url = parent_link+"&what=view&which_part=thish&which_year="+yid+"&which_fid="+fid+"&which_bid="+bid;
		    sbf.append(MyHtml.makeLinkCell("View", url));
		    url = parent_link+"&what=edit&which_part=thish&which_year="+yid+"&which_fid="+fid+"&which_bid="+bid;
		    sbf.append(MyHtml.makeLinkCell("Edit", url));

		    sbf.append("<TD align=center>"+getBrokerName(bid)+"</TD>");
		    sbf.append("<TD align=right>"+td.format(Double.parseDouble(t_sold))+"</TD>");
		    sbf.append("<TD align=center>"+status+"</TD>");
		    sbf.append("</TR>");
		    
		    cyear  = yid;
		}

		sbf.append("<TR>");
		sbf.append("<TD align=center>&nbsp;</TD>");
		sbf.append("<TD align=center>&nbsp;</TD>");
		sbf.append("<TD align=center><B>"+cyear+"</B></TD>");
			
		url = parent_link+"&what=view&which_part=preh&which_year="+cyear+"&okid="+down;
		sbf.append("<TD align=center>"+MyHtml.makeLinkButton("View", url)+"</TD>");
		url = parent_link+"&what=edit&which_part=preh&which_year="+cyear+"&okid="+down;
		sbf.append("<TD align=center>"+MyHtml.makeLinkButton("Edit", url)+"</TD>");

		url = parent_link+"&what=view&which_part=thish&which_year="+cyear+"&okid="+down;
		sbf.append("<TD align=center>"+MyHtml.makeLinkButton("View", url)+"</TD>");
		url = parent_link+"&what=edit&which_part=thish&which_year="+cyear+"&okid="+down;
		sbf.append("<TD align=center>"+MyHtml.makeLinkButton("Edit", url)+"</TD>");

		sbf.append("<TD align=center>&nbsp;</TD>");
		sbf.append("<TD align=right>"+td.format(y_total)+"</TD>");
		
		if(down) {
		    sbf.append("<TD align=center><img src=/images/star_green.gif></TD>");
		}
		else {
		    sbf.append("<TD align=center><img src=/images/star.gif></TD>");
		}
		sbf.append("</TR>");
		
		sbf.append("</TABLE>");
	    }
	}
	catch (Exception e) {
	    sbf.append(e);
	}
	return sbf;
    }

    private StringBuffer getEditUserName(String call_path, String mid, String aid, String name) {
	StringBuffer sbf = new StringBuffer();
	try {
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }

	    ArrayList a = ejbdb.getScheduleDUsers();
	    sbf.append("<FORM method=post action="+call_path+">");
	    sbf.append("<TABLE>");
	    sbf.append("<TR>");
	    sbf.append("<TD align=center>"+MyHtmlForm.getSelect(name, a, "")+"</TD></TR>");
	    sbf.append("<TR><TD align=center>");
	    sbf.append("<BR><input type=submit name=Submit value='Get User 1099 Records' style='font-size:8pt'></TD>");
	    sbf.append("<TR>");
	    sbf.append("</TABLE>");
	    sbf.append("<input type=hidden name=mid value="+mid+">");
	    sbf.append("<input type=hidden name=aid value="+aid+">");
	    sbf.append("</FORM>");
	}
	catch (Exception e) {
	    return sbf.append(e);
	}
	return sbf;
    }

    private Hashtable get1099IdAndBroker (String who, String which_year) {
	Hashtable fbhs = new Hashtable();
	try {
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }
	    
	    ArrayList a = null;
	    ArrayList alist = ejbdb.selectUserScheduleDInfo(who);
	    if(alist != null && !alist.isEmpty()) {
		Iterator it1 = alist.iterator();
		Iterator it2 = null;
		String sfid    = null;
		String sbid    = null;
		String syid   = null;
		String st_sold = null;
		String sstatus = null;
		String url = null;
	
		while (it1.hasNext()) {
		    a = (ArrayList)it1.next();
		    it2    = a.iterator();
		    sfid    = (String)a.get(0);
		    sbid    = (String)a.get(1);
		    st_sold = (String)a.get(2);
		    syid    = (String)a.get(3);
		    sstatus = (String)a.get(4);
		    
		    if(syid.equals(which_year)) {
			fbhs.put(sfid, sbid);
		    }
		}
	    }
	}
	catch (Exception e) {
	    System.out.println("ScheduleDAdmin:get1099IDAndBroker:"+e);
	}
	return fbhs;
    }

    private StringBuffer viewHolding(HttpServletRequest request, String what, String who, String which_year, 
				     String which_part, String which_bid, String which_fid) {
	StringBuffer sbf = new StringBuffer();
	if(which_year == null) {
	    return sbf.append("Year not selected.");
	}

	if(which_bid != null && which_fid != null) {
	    if(which_part != null && which_part.equals("preh")) {
		return viewPreYearHolding(request, who, which_year, which_bid, which_fid);
	    }
	    if(which_part != null && which_part.equals("thish")) {
		return viewThisYearHolding(request, who, which_year, which_bid, which_fid);
	    }
	}
	else {
	    Hashtable fbhs = get1099IdAndBroker(who, which_year);
	    Enumeration enum = fbhs.keys();
	    String sfid = null;
	    String sbid = null;
	    while(enum.hasMoreElements()) {
		sfid = (String)enum.nextElement();
		sbid = (String)fbhs.get(sfid);
		
		if(which_part != null && which_part.equals("preh")) {
		    sbf.append(viewPreYearHolding(request, who, which_year, sbid, sfid));
		}
	    
		if(which_part != null && which_part.equals("thish")) {
		    sbf.append(viewThisYearHolding(request, who, which_year, sbid, sfid));
		}
	    }
	}
	return sbf;
    }

    private StringBuffer viewPreYearHolding(HttpServletRequest request, String sd_user, String yid, String bid, String fid) {

	String record_status = "3";
	StringBuffer sbf = new StringBuffer();
	Vector v = null;

	try {
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }
	    if(isBaseYear(sd_user, bid, yid)) {
		v = ejbdb.selectScheduleDDetailRecords(fid, record_status, 0); // 0: order by date
	    }
	    else{
		v = ejbdb.getLastYearDetailHoldingRecords(sd_user, yid, bid);
	    }
	}
	catch (Exception e) {
	    System.out.println("ScheduleDAdmin:adminPreYearHolding:"+e);
	}

	sbf.append("<TABLE width=90% border=1 cellSpacing=0 cellPadding=2>");
	sbf.append("<TR><TD align=center colspan=6><BR><H3>PRE-YEAR ["+yid+"] HOLDING ");
	sbf.append(" BROKER ["+getBrokerName(bid)+"]</H3><BR></TD></TR>");
	sbf.append("</TD></TR>");	

	sbf.append("<TR>");
	sbf.append("<TD align=center>BOUGHT/SOLD</TD>");
	sbf.append("<TD align=center>COST</TD>");
	sbf.append("<TD align=center>TICK</TD>");
	sbf.append("<TD align=center>SHARES</TD>");
	sbf.append("<TD align=center>PRICE</TD>");
	sbf.append("<TD align=center>DATE</TD>");
	sbf.append("<TR>");

	if(v == null || v.isEmpty()) {
	    return sbf.append("<TR><TD align=center colspan=6>No Pre-Year Holding Data.</TD></TR></TABLE>");
	}

	Enumeration enum = v.elements();
	String bs    = null;
	double cost  = 0.0;
	String tick  = null;
	int share    = 0;
	double price = 0.0;
	String date  = null;
	
	try {
	    while(enum.hasMoreElements()) {
		bs    = (String)enum.nextElement();
		cost  = Double.parseDouble(""+enum.nextElement());
		tick  = (String)enum.nextElement();
		share = (new Double(""+enum.nextElement())).intValue();
		price = Double.parseDouble(""+enum.nextElement());
		date  = (String)enum.nextElement();
		
		sbf.append("<TR>");
		sbf.append("<TD align=center>"+bs+"</TD>");
		sbf.append("<TD align=right>"+td.format(java.lang.Math.abs(cost))+"</TD>");
		sbf.append("<TD align=center>"+tick+"</TD>");
		sbf.append("<TD align=right>"+share+"</TD>");
		sbf.append("<TD align=right>"+td.format(price)+"</TD>");
		sbf.append("<TD align=center>"+date+"</TD>");
		sbf.append("</TR>");
	    }
	}
	catch (Exception e) {
	    System.out.println("ScheduleDAdmin:adminPreYearHolding:"+e);
	}

	sbf.append("</TABLE>");
	enum = null;
	v    = null;
	return sbf;
    }

    private StringBuffer viewThisYearHolding (HttpServletRequest request, String sd_user, String yid, String bid, String fid) {
	StringBuffer sbf = new StringBuffer();
	if(yid == null || bid == null || fid == null || sd_user == null) {
	    return sbf.append("View This Year Error.");
	}

	try {
	    sbf.append("<TABLE width=90% border=1 cellSpacing=0 cellPadding=2>");
	    sbf.append("<TR><TD align=center colspan=6>");
	    sbf.append("<BR><H3>YEAR ["+yid+"] BROKER ["+getBrokerName(bid)+"] HOLDING RECORDS</H3>");
	    sbf.append("</TD>");
	    sbf.append("</TR>");
	    sbf.append("<TR><TD align=center>BOUGHT/SOLD</TD><TD align=center>COST</TD>");
	    sbf.append("<TD align=center>TICK</TD><TD align=center>SHARES</TD>");
	    sbf.append("<TD align=center>PRICE</TD><TD align=center>DATE</TD></TR>");
	    
	    Vector vr = ejbdb.selectHoldingRecords(fid, sd_user, yid, bid); 
	    if(vr != null) {
		StockRecord r = null;
		Enumeration enum = vr.elements();
		while(enum.hasMoreElements()) {
		    r = (StockRecord)enum.nextElement();
		    sbf.append("<TR>");
		    sbf.append("<TD align=center>"+r.getBuySell()+"</TD>");
		    sbf.append("<TD align=right>"+td.format(java.lang.Math.abs(r.getCost()))+"</TD>");
		    sbf.append("<TD align=center>"+r.getTick()+"</TD>");
		    sbf.append("<TD align=right>"+r.getShare()+"</TD>");
		    sbf.append("<TD align=right>"+td.format(r.getPrice())+"</TD>");
		    sbf.append("<TD align=center>"+r.getDate()+"</TD>");
		    sbf.append("</TR>");
		}
	    }

	    sbf.append("</TABLE>");
	    return sbf;
	}
	catch (Exception e) {
	    System.out.println("ScheduleDAmind:adminThisYearHolding:Error:"+e);
	}
	return null;
    }

    private StringBuffer editHolding(HttpServletRequest request, String call_path, String mid, String aid, 
				     String what, String who, String which_year, String which_part, 
				     String which_bid, String which_fid) {

	StringBuffer sbf = new StringBuffer();

	if(which_year != null && which_bid != null && which_fid != null) {
	    /*
	      if(where != null && where.equals("preh")) {
	      return viewPreYearHolding(request, user, yid, bid, fid);
	    }
	    if(where != null && where.equals("thish")) {
		return editThisYearHolding(request, call_path, mid, aid, what, where, user, yid, bid, fid);
	    }
	    */
	    return sbf.append("call single year edit");
	}
	else {
	    if(which_part != null && which_part.equals("preh")) {
		//sbf.append(editThisYearHolding(request, call_path, mid, aid, what, user, yid, bid, fid));
		return sbf.append("call full year pre-year edit");
	    }

	    if(which_part != null && which_part.equals("thish")) {
		sbf.append("<H3>User: ["+who+"] Year: ["+which_year+"] PART: [THIS HOLDING] </H3>");
		sbf.append(editThisYearHolding(request, call_path, mid, aid, what, which_part, who, which_year));
	    }
	    return sbf;
	}
    }

    private StringBuffer editThisYearHolding (HttpServletRequest request, String call_path, String mid, String aid, String what,
					      String where, String sd_user, String yid, String bid, String fid) {

	StringBuffer sbf = new StringBuffer();
	try {
	    sbf.append(viewThisYearHolding(request, sd_user, yid, bid, fid));
	    String query = "SELECT * FROM TAX_SCHEDULE_D_HOLDING WHERE USER_NAME='"+sd_user+"' AND YEAR="+yid+" AND BROKER_ID="+bid+";";
	    ArrayList alist = ejbdb.select(query);
	    ArrayList a = null;
	    Iterator it = alist.iterator();
	    sbf.append("<FORM method= post action="+call_path+">");
	    sbf.append("<TABLE width=90% border=1 cellSpacing=0 cellPadding=2>");

	    Iterator it2 = null;
	    while (it.hasNext()) {
		a = (ArrayList)it.next();
		it2 = a.iterator();
		sbf.append("<TR>");
		while(it2.hasNext()) {
		    sbf.append("<TD>"+it2.next()+"</TD>");
		}
		sbf.append("</TR>");
	    }

	    sbf.append("</TABLE>");
	    sbf.append("</FORM>");
	}
	catch (Exception e) {
	    System.out.println("ScheduleDAmind:adminThisYearHolding:Error:"+e);
	}
	return sbf;
    }

    private StringBuffer editThisYearHolding (HttpServletRequest request, String call_path, String mid, String aid, String what,
					      String which_part, String who, String which_year) {

	StringBuffer sbf = new StringBuffer();
	try {
	    String sfid   = null;
	    String sbid   = null;

	    String rid    = null;
	    String rfid   = null;
	    String ryid   = null;
	    String rbid   = null;
	    String ruid   = null;
	    String rbs    = null;
	    String rcost  = null;
	    String rtick  = null;
	    String rshare = null;
	    String rprice = null;
	    String rdate  = null;

	    ArrayList aa  = null;
	    ArrayList a   = null;
	    String query  = null;
	    Iterator it   = null;

	    Hashtable fbhs = get1099IdAndBroker(who, which_year);

	    Enumeration enum = fbhs.keys();
	    sbf.append("<TABLE width=90% border=1 cellSpacing=0 cellPadding=2>");
	    while (enum.hasMoreElements()) {
		sfid = (String)enum.nextElement();
		sbid = (String)fbhs.get(sfid);

		sbf.append("<TR>");
		sbf.append("<TD>MOVE</TD>");
		sbf.append("<TD>ID</TD>");
		sbf.append("<TD>FID</TD>");
		sbf.append("<TD>YEAR</TD>");
		sbf.append("<TD>BROKER</TD>");
		sbf.append("<TD>USER</TD>");
		sbf.append("<TD>B/S</TD>");
		sbf.append("<TD>COST</TD>");
		sbf.append("<TD>TICK</TD>");
		sbf.append("<TD>SHARES</TD>");
		sbf.append("<TD>PRICE</TD>");
		sbf.append("<TD>DATE</TD>");
		sbf.append("<TD>ADD</TD>");
		sbf.append("</TR>");

		query = "SELECT * FROM TAX_SCHEDULE_D_HOLDING WHERE USER_NAME='"+
		    who+"' AND YEAR="+which_year+" AND BROKER_ID="+sbid+";";

		aa = ejbdb.select(query);
		it = aa.iterator();
		while (it.hasNext()) {
		    a  = (ArrayList)it.next();
		    rid    = (String)a.get(0);
		    rfid   = (String)a.get(1);
		    ryid   = (String)a.get(2);
		    rbid   = (String)a.get(3);
		    ruid   = (String)a.get(4);
		    rbs    = (String)a.get(5);
		    rcost  = (String)a.get(6);
		    rtick  = (String)a.get(7);
		    rshare = (String)a.get(8);
		    rprice = (String)a.get(9);
		    rdate  = (String)a.get(10);

		    sbf.append("<FORM method=post action="+call_path+">");
		    sbf.append("<TR>");
		    sbf.append("<TD align=center><input type=submit name=submit value=Try style='font-size:8pt'></TD>");
		    sbf.append("<TD>"+rid+"</TD>");
		    sbf.append("<TD align=center><input type=text name=rfid value="+rfid+" size=5 style='font-size:8pt'></TD>");
		    sbf.append("<TD align=center>"+ryid+"</TD>");
		    sbf.append("<TD align=center><input type=text name=rbid value="+rbid+" size=5 style='font-size:8pt'></TD>");
		    sbf.append("<TD align=center>"+ruid+"</TD>");
		    sbf.append("<TD align=center>"+rbs+"</TD>");
		    sbf.append("<TD align=right>"+rcost+"</TD>");
		    sbf.append("<TD align=center>"+rtick+"</TD>");
		    sbf.append("<TD align=right>"+rshare+"</TD>");
		    sbf.append("<TD align=right>"+td.format(Double.parseDouble(rprice))+"</TD>");
		    sbf.append("<TD align=right>"+rdate+"</TD>");
		    sbf.append("<TD align=center><input type=submit name=submit value=Add style='font-size:8pt'></TD>");
		    sbf.append("</TR>");
		    sbf.append("<input type=hidden name=mid value="+mid+">");
		    sbf.append("<input type=hidden name=aid value="+aid+">");
		    sbf.append("<input type=hidden name=what value="+what+">");
		    sbf.append("<input type=hidden name=which_part value="+which_part+">");
		    sbf.append("<input type=hidden name=which_year value="+which_year+">");
		    sbf.append("<input type=hidden name=who value="+who+">");
		    sbf.append("<input type=hidden name=rid value="+rid+">");
		    sbf.append("</FORM>");
		}
	    }
	    sbf.append("</TABLE>");
	    sbf.append("* Move: Move record between two brokers.<BR>");
	    sbf.append("** Add: Add record to this year detail records and reset status to generate new data.");
	    sbf.append("<BR>????????.");
	    enum = null;
	    fbhs = null;
	    sfid = null;
	    sbid = null;
	    aa = null;
	    it = null;
	    a  = null;
	    query  = null;
	    rid    = null;
	    rfid   = null;
	    ryid   = null;
	    rbid   = null;
	    ruid   = null;
	    rbs    = null;
	    rcost  = null;
	    rtick  = null;
	    rshare = null;
	    rprice = null;
	    rdate  = null;
	}
	catch (Exception e) {
	    System.out.println("ScheduleDAmind:editThisYearHolding:Error:"+e);
	}
	return sbf;
    }

    private String updateHolding(HttpServletRequest request) {
	String current_id   = request.getParameter("rid");
	String new_fid      = request.getParameter("rfid");
	String new_bid      = request.getParameter("rbid");
	String which_year   = request.getParameter("which_year");
	String who          = request.getParameter("who");
	String which_part   = request.getParameter("which_part");
	String error = null;
	try {
	    if(which_part != null && which_part.equals("thish")) {
		String query = "SELECT FID, BROKER_ID, BUY_SELL, COST, TICK, SHARES, DATE FROM TAX_SCHEDULE_D_HOLDING "+
		    " WHERE ID="+current_id+" AND USER_NAME='"+who+"';";
		ArrayList a1 = ejbdb.selectSingleRecord(query);
		if(a1 == null || a1.isEmpty()) {
		    error = "Record not found.";
		}
		else {
		    //ID(*) FID YEAR BROKER_ID USER_NAME BUY_SELL COST TICK SHARES PRICE DATE 
		    String current_fid = (String)a1.get(0);
		    String current_bid = (String)a1.get(1);
		    String bs          = (String)a1.get(2);
		    String cost        = (String)a1.get(3);
		    String tick        = (String)a1.get(4);
		    String share       = (String)a1.get(5);
		    String date        = (String)a1.get(6);
		    
		    if(current_fid.equals(new_fid)) {
			error = "You must enter a new FID.";
		    }

		    if(error == null) {
			query = "SELECT * FROM TAX_SCHEDULE_D_HOLDING "+
			    " WHERE YEAR ="+which_year+" AND FID="+new_fid+" AND BROKER_ID="+new_bid+
			    " AND USER_NAME='"+who+"' ORDER BY DATE;";
			
			ArrayList a2 = ejbdb.selectSingleRecord(query);
			if(a2 == null || a2.isEmpty()) {
			    error = "No record for FID="+new_fid+" and BROKER_ID="+new_bid+" For User "+who;
			} 
			else {
			    query = "UPDATE TAX_SCHEDULE_D_HOLDING SET FID="+new_fid+", BROKER_ID="+new_bid+
				" WHERE ID="+current_id+" AND USER_NAME='"+who+"';";
			    if(ejbdb.update(query) < 1) {
				error = "Error:"+query;
			    }
			}
		    }
		}
	    }
	}
	catch (Exception e) {

	}
	return error;
    }

    private String addHoldingToDetail(HttpServletRequest request) {
	String id           = request.getParameter("rid");
	String which_year   = request.getParameter("which_year");
	String who          = request.getParameter("who");
	String which_part   = request.getParameter("which_part");
	String error = null;
	
	try {
	    if(which_part != null && which_part.equals("thish")) {
		String query = "SELECT FID, BROKER_ID, BUY_SELL, COST, TICK, SHARES, PRICE, DATE FROM TAX_SCHEDULE_D_HOLDING "+
		    " WHERE ID="+id+" AND USER_NAME='"+who+"';";
		ArrayList a1 = ejbdb.selectSingleRecord(query);
		if(a1 == null || a1.isEmpty()) {
		    error = "Record not found.";
		}
		else {
		    // TAX_SCHEDULE_D_DETAIL
		    // ID(*) FID USER_NAME YEAR BROKER_ID STATUS BUY_SELL COST TICK SHARES PRICE DATE
		    String fid         = (String)a1.get(0);
		    String bid         = (String)a1.get(1);
		    String bs          = (String)a1.get(2);
		    String cost        = (String)a1.get(3);
		    String tick        = (String)a1.get(4);
		    String share       = (String)a1.get(5);
		    String price       = (String)a1.get(6);
		    String date        = (String)a1.get(7);
		    int status      = 0;

		    update1099Status(who, which_year, bid, 2);
		    Vector vv = new Vector ();
		    Vector v = new Vector();
		    v.add(bs);
		    v.add(cost);
		    v.add(tick);
		    v.add(share);
		    v.add(price);
		    v.add(date);
		    vv.add(v);

		    if(ejbdb.insertScheduleDDetailRecords(Integer.parseInt(fid), who, Integer.parseInt(which_year), 
							  Integer.parseInt(bid), status, vv)<0) {
			error = "Transfer data error.";
		    }
		    else {
			query = "DELETE FROM TAX_SCHEDULE_D_HOLDING WHERE ID="+id;
			ejbdb.delete(query);
		    }
		}
	    }
	}
	catch (Exception e) {

	}
	return error;
    }
}









